package andrei.mower2.business;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;

import java.util.SortedSet;

import org.junit.Test;

import andrei.mower2.business.MowerDriver;
import andrei.mower2.utils.TestUtils;
import andrei.mower2.utils.MockUtils;

public class MowerDriverIT {

	@Test
	public void driveMowersTest() throws IOException {
		String inputFileContent = TestUtils.generateFileContent(
				"5 5",
				"1 2 N",
				"GAGAGAGAA",
				"3 3 E",
				"AADAADADDA");
		
		SortedSet<String> outputLines = new TreeSet<>();
		MowerDriver mowerDriver = new MowerDriver(MockUtils.getMowerStringInputReader(inputFileContent), 
				MockUtils.getStringResultOutputter(outputLines));
		
		mowerDriver.driveMowers();
		
		assertEquals(2, outputLines.size());
		Iterator<String> iterator = outputLines.iterator();
		assertEquals("1 3 N", iterator.next());
		assertEquals("5 1 E", iterator.next());
	}
	
	@Test
	public void driveMowersDriveOutsideLawnTest() throws IOException {
		String inputFileContent = TestUtils.generateFileContent(
				"5 5",
				"1 1 N",
				"GAAADAA");
		
		SortedSet<String> outputLines = new TreeSet<>();
		MowerDriver mowerDriver = new MowerDriver(MockUtils.getMowerStringInputReader(inputFileContent), 
				MockUtils.getStringResultOutputter(outputLines));
		
		mowerDriver.driveMowers();
		
		Iterator<String> iterator = outputLines.iterator();
		assertEquals("0 3 N", iterator.next());
	}
	
	@Test
	public void driveMowersPlaceOutsideLawnTest() throws IOException {
		String inputFileContent = TestUtils.generateFileContent(
				"5 5",
				"7 1 N",
				"GAAADAA",
				"3 3 E",
				"AADAADADDA");
		
		SortedSet<String> outputLines = new TreeSet<>();
		MowerDriver mowerDriver = new MowerDriver(MockUtils.getMowerStringInputReader(inputFileContent), 
				MockUtils.getStringResultOutputter(outputLines));
		
		mowerDriver.driveMowers();
		
		assertEquals(1, outputLines.size());
		Iterator<String> iterator = outputLines.iterator();
		assertEquals("5 1 E", iterator.next());
	}
	
	@Test
	public void driveMowersInvalidCommandTest() throws IOException {
		String inputFileContent = TestUtils.generateFileContent(
				"5 5",
				"1 2 N",
				"GAGAGAG?A",
				"3 3 E",
				"AADAADADDA");
		
		SortedSet<String> outputLines = new TreeSet<>();
		MowerDriver mowerDriver = new MowerDriver(MockUtils.getMowerStringInputReader(inputFileContent), 
				MockUtils.getStringResultOutputter(outputLines));
		
		mowerDriver.driveMowers();
		
		assertEquals(2, outputLines.size());
		Iterator<String> iterator = outputLines.iterator();
		assertEquals("1 2 N", iterator.next());
		assertEquals("5 1 E", iterator.next());
	}
	
	@Test
	public void driveMowersEmptyLinesTest() throws IOException {
		String inputFileContent = TestUtils.generateFileContent(
				"5 5",
				"1 2 N",
				"GAGAGAGAA",
				"",
				"3 3 E",
				"AADAADADDA");
		
		SortedSet<String> outputLines = new TreeSet<>();
		MowerDriver mowerDriver = new MowerDriver(MockUtils.getMowerStringInputReader(inputFileContent), 
				MockUtils.getStringResultOutputter(outputLines));
		
		mowerDriver.driveMowers();
		
		assertEquals(1, outputLines.size());
		Iterator<String> iterator = outputLines.iterator();
		assertEquals("1 3 N", iterator.next());
	}
}
